using System;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace CKS.EBE.WebControls
{
    public class BaseForm : TemplateBasedControl
    {
        protected string EmailVal { get { return TextBoxValue("idEmail"); } }
        protected string NameVal { get { return TextBoxValue("idName"); } }
        protected string ErrorLabel { set { SetLabel("idError", value); } }

        protected PlaceHolder FormControl
        {
            get
            {
                Control c = TemplateContainer.FindControl("idForm");
                if (c is PlaceHolder)
                    return (PlaceHolder)c;

                return null;
            }
        }

        protected PlaceHolder ResponseControl
        {
            get
            {
                Control c = TemplateContainer.FindControl("idResponse");
                if (c is PlaceHolder)
                    return (PlaceHolder)c;

                return null;
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Control c = TemplateContainer.FindControl("idSubmit");
            if (c is Button)
            {
                Button Submit = (Button)c;
                Submit.Click += new EventHandler(Submit_Click);
            }
        }

        void Submit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                OnSubmit(sender, e);
            }
        }

        protected virtual void OnSubmit(object sender, EventArgs e)
        {
        }

        protected virtual string TextBoxValue(string textBoxID)
        {
            Control c = TemplateContainer.FindControl(textBoxID);
            if (c is TextBox)
            {
                TextBox tb = (TextBox)c;
                return tb.Text;
            }

            return "";
        }

        protected virtual Control FindFormControl(string controlID)
        {
            return TemplateContainer.FindControl(controlID);
        }

        protected virtual void SetLabel(string labelID, string value)
        {
            Control c = TemplateContainer.FindControl(labelID);
            if (c is Label)
            {
                Label l = (Label)c;
                l.Text = value;
            }
        }
    }
}
